<?php
class My_Utility
{
	private static $logger;
	/**
	 * get logger
	 *
	 * @return Zend_Log logger
	 */
	public static function getLogger()
	{
		if(isset(self::$logger))
		{
			return self::$logger;
		}
		$writer = new Zend_Log_Writer_Stream(APPLICATION_PATH."/resource/log.txt");
		$logger = new Zend_Log($writer);
		return $logger;
	}
	
	public static function log($text)
	{
		$logger = self::getLogger();
		$logger->info($text);
	}
		
	public static function createThumb($fileName,$new_w,$new_h)
	{
		$ext=end(explode('.',$fileName));
		if (preg_match('/jpg|jpeg/',$ext)){
			$src_img=imagecreatefromjpeg($fileName);
		}
		if (preg_match('/png/',$ext)){
			$src_img=imagecreatefrompng($fileName);
		}
		$old_x=imageSX($src_img);
		$old_y=imageSY($src_img);
		if($old_x <= $new_w || $old_y <= $new_h)
		{
			return false;
		}
		if ($old_x > $old_y) {
			$thumb_w=$new_w;
			$thumb_h=$old_y*($new_h/$old_x);
		}
		if ($old_x < $old_y) {
			$thumb_w=$old_x*($new_w/$old_y);
			$thumb_h=$new_h;
		}
		if ($old_x == $old_y) {
			$thumb_w=$new_w;
			$thumb_h=$new_h;
		}
		$dst_img=ImageCreateTrueColor($thumb_w,$thumb_h);
		imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y);
		$thumbFile = "thumb_".basename($fileName);
		
		if (preg_match("/png/",$ext))
		{
			imagepng($dst_img,dirname($fileName)."/".$thumbFile);
		} else {
			imagejpeg($dst_img,dirname($fileName)."/".$thumbFile);
		}
		imagedestroy($dst_img);
		imagedestroy($src_img);
		return $thumbFile;
	}
	
}
?>